package com.heima.stroke.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.heima.modules.vo.StrokeVO;
import com.heima.stroke.configuration.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;



@Component
public class MQProducer {
    private final static Logger logger = LoggerFactory.getLogger(MQProducer.class);

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 发送延时订单MQ
     *
     * @param strokeVO
     */
    public void sendOver(StrokeVO strokeVO) {  try {
        String json = JSON.toJSONString(strokeVO);
        Message message = MessageBuilder.withBody(json.getBytes())
                .setContentType(org.springframework.amqp.core.MessageProperties.CONTENT_TYPE_JSON)
                .build();
        rabbitTemplate.convertAndSend(
                RabbitConfig.STROKE_DEAD_QUEUE_EXCHANGE,
                RabbitConfig.STROKE_DEAD_KEY,
                message
        );
        logger.info("Sent stroke message to dead letter queue: {}", strokeVO);
    } catch (Exception e) {
        logger.error("Failed to send stroke message to dead letter queue: {}", strokeVO, e);
    }
    }
}
